package cn.wolfcode.business.utils;

import javax.sound.midi.Soundbank;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    //将字符串转为date类型
    public static Date parseDate(String strDate, String regex) {
        if (strDate != null && regex != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(regex);
            try {
                return sdf.parse(strDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.out.println("测试");
        return null;
    }

    //+1天 -1秒
    public static Date getEndTime(Date date) {
        if (date == null) {
            return null;
        }
        //日期处理
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //+1天
        calendar.add(Calendar.DAY_OF_YEAR,1);
        //-1秒
        calendar.add(Calendar.SECOND,-1);
        return calendar.getTime();
    }

    //将date类型转为字符串类型
    public static String formatDate(Date date, String regex){
        if(date == null || regex == null){
            return  null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(regex);
        return sdf.format(date);
    }
}
